package com.powerunion.datacollection.web.struts;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.powerunion.datacollection.model.MaintblData;
import com.powerunion.datacollection.service.FormSetManager;
import com.powerunion.datacollection.service.MaintblDataManager;
import com.powerunion.datacollection.transaction.Transaction;
import com.powerunion.datacollection.transaction.TransactionHelper;
import com.powerunion.util.log.LogHelper;

public class FormSetAction extends BaseAction {

	private static final Logger logger = LogHelper.getInstance().getLogger(
			FormSetAction.class);
	private FormSetManager formSetManager = null;

	public FormSetAction() {
		super();
		this.formSetManager = new FormSetManager();
	}

	public void finalize() throws Throwable {
		this.formSetManager = null;
		super.finalize();
	}

	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String saveStr = request.getParameter("formData");
		
		Transaction transaction = null;
		try {
			transaction = TransactionHelper.getTransaction();
			transaction.begin();
			logger.debug("主表信息:" + saveStr);

			boolean isSuccess = false;
			isSuccess = this.formSetManager.save(saveStr); 

			if (isSuccess) {
				response.getWriter().write("{success:true}");
			} else {
				response.getWriter().write("{failure:true}");
			}

			transaction.commit();
		} catch (Exception e) {
			try {
				logger.error("", e);
				transaction.rollback();
			} catch (Exception e1) {
				logger.error("", e1);
				throw e;
			} finally {
				transaction = null;
				response.getWriter().write("{failure:true}");
			}
		}

		return null;
	}

	public ActionForward loadInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String id = request.getParameter("id");
		response.setContentType("text/json;charset=utf-8");
		String data = this.formSetManager.loadInfo(id);
		logger.debug("loadInfo the FormSet:" + data);
		response.getWriter().print(data);
		return null;
	}
	/**
	 * 加载从表设置信息
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward loadSubInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String id = request.getParameter("id");
		response.setContentType("text/json;charset=utf-8");
		String data = this.formSetManager.loadInfo(id);
		logger.debug("loadInfo the sub FormSet:" + data);
		response.getWriter().print(data);
		return null;
	}
	
	
	public ActionForward getDataSourceTree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		JSONArray jsonArray = new JSONArray();
		response.setContentType("text/json;charset=utf-8");
		String syrName = request.getParameter("node");
		jsonArray = this.formSetManager.getDataSourceTree(syrName);
		try {
			response.getWriter().print(jsonArray);
		} catch (IOException e) {
			logger.error("输出json失败" ,e);
			return null;
		}
		return null;
	}

	public ActionForward getFieldNames(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String tableName = request.getParameter("id");
		String svrName = request.getParameter("svrName");
		String result = "";
		result = this.formSetManager.getFieldNames(svrName, tableName);
		response.setContentType("text/json;charset=utf-8");
		try {
			response.getWriter().print(result);
		} catch (Exception e) {
			logger.error("输出json失败", e);
			return null;
		}
		return null;
	}
	
	public ActionForward getFields(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String formId = request.getParameter("formId");
		String result = "";
		result = this.formSetManager.getFields(formId);
		logger.debug("getFields:"+result);
		response.setContentType("text/json;charset=utf-8");
		try {
			response.getWriter().print(result);
		} catch (Exception e) {
			logger.error("输出json失败", e);
			return null;
		}
		return null;
	}
	
	public ActionForward getServerFields(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String svrName = request.getParameter("svrName");
		String tableName = request.getParameter("tableName");
		String result = "";
		result = this.formSetManager.getServersFields(svrName, tableName);
		response.setContentType("text/json;charset=utf-8");
		try {
			response.getWriter().print(result);
		} catch (Exception e) {
			logger.error("输出json失败", e);
			return null;
		}
		return null;
	}
	
	public ActionForward getServerTables(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String svrName = request.getParameter("svrName");
		String result = "";
		result = this.formSetManager.getServerTables(svrName);
		response.setContentType("text/json;charset=utf-8");
		try {
			response.getWriter().print(result);
		} catch (Exception e) {
			logger.error("输出json失败", e);
			return null;
		}
		return null;
	}
	
	public ActionForward getComboFields(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String svrName = request.getParameter("svrName");
		String tblName = request.getParameter("tblName");
		String itemLabel = request.getParameter("itemLabel");
		String itemValue = request.getParameter("itemValue");
		String result = "";
		result = this.formSetManager.getComboFields(svrName, tblName, itemLabel, itemValue);
		response.setContentType("text/json;charset=utf-8");
		try {
			response.getWriter().print(result);
		} catch (Exception e) {
			logger.error("输出json失败", e);
			return null;
		}
		return null;
	}
	
	
	public ActionForward getMaintblDataInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		String formId = request.getParameter("formId");
		String submiUser = request.getRemoteUser();
		String procId = request.getParameter("procId");
		String actId = request.getParameter("actId");
		
		MaintblData data = new MaintblData();
		data.setFormId(formId);
		data.setSubmiUser(submiUser);
		data.setProcId(procId);
		data.setActId(actId);
		
		MaintblDataManager mdm = new MaintblDataManager();
		JSONObject jsonObject = mdm.getMaintblDataInfo(data); 
		String result = "{success:true,data:[" + jsonObject + "]}";
		logger.debug("getMaintblDataInfo:"+result);
		response.setContentType("text/json;charset=utf-8");
		try {
			response.getWriter().print(result);
		} catch (Exception e) {
			logger.error("输出json失败", e);
			return null;
		}
		return null;
	}
	
	public ActionForward getMainFormSetDataSourceTree(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String formId = request.getParameter("formId");
		String syrName = request.getParameter("node");
		JSONArray jsonArray = new JSONArray();
		response.setContentType("text/json;charset=utf-8");
		jsonArray = this.formSetManager.getMainFormSetDataSourceTree(formId,syrName);
		try {
			response.getWriter().print(jsonArray);
		} catch (IOException e) {
			logger.error("输出json失败" ,e);
			return null;
		}
		return null;
	}
	
	
}
